package com.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blog.mapper.UserMapper;
import com.blog.pojo.User;
import com.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 验证登录
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public User checkUser(String username, String password) {
        User user = userMapper.selectOne(new QueryWrapper<User>()
                .eq("username", username)
                .eq("password", (password)));
        return user;
    }

    @Override
    public User checkFace(String face) {
        return null;
    }

    @Override
    public void addUser(String username, String password) {

    }

    @Override
    public void registerUser(String username, String email, String password) {
        User user = new User();
        user.setUsername(username);
        user.setEmail(email);
        user.setNickname(username);
        user.setPassword(password);
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        user.setCreateTime(new Date(System.currentTimeMillis()));
        user.setUpdateTime(new Date(System.currentTimeMillis()));
        userMapper.insert(user);
    }

    @Override
    public boolean isUserExists(String username) {
        // 使用 MyBatis Plus 的 QueryWrapper 进行条件查询
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", username));

        // 如果user不为null，说明用户名存在
        return user != null;
    }
    @Override
    public boolean isEmailExists(String email) {
        // 使用 MyBatis Plus 的 QueryWrapper 进行条件查询
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("email", email));

        // 如果user不为null，说明邮箱存在
        return user != null;
    }

}
